package cn.work.controller;


import cn.work.entity.User;
import cn.work.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("test")
public class CrudController {
    @Autowired
    private CrudService crudService;
    @RequestMapping("index")
    public String index(){
        return "success";
    }
    @RequestMapping("/list")
    public String UserList(HttpServletRequest request) {

        List<User> list =crudService.findAllUser();
        //传递数据至前端
        request.setAttribute("user",list);
        //返回对应视图
        return "itemList";
    }
@RequestMapping("/edit")
public String edit(int id,HttpServletRequest request){
        User user=crudService.findUserById(id);
        request.setAttribute("user",user);
        return "edit";
}
    //保存数据到数据库后跳转到列表页面
    @RequestMapping("/updateUser")
    public String editUserInfo(User user)
    {
        crudService.editUser(user);
        return "redirect:list";
    }

    @RequestMapping("/deleteByID")
    public String deleteByID(int id){
        crudService.clearById(id);
        return "redirect:list";
    }

    @RequestMapping("/add")
    public String add()
    {
        return "AddUser";
    }
    //保存数据到数据库后跳转到列表页面
    @RequestMapping("/incerseUser")
    public String addUser(User user)
    {
        crudService.incerseUser(user);
        return "redirect:list";
    }
}
